package com.tang.managementdemo.controller;

import com.tang.managementdemo.pojo.PageResult;
import com.tang.managementdemo.pojo.Result;
import com.tang.managementdemo.pojo.StuQueryParam;
import com.tang.managementdemo.pojo.Student;
import com.tang.managementdemo.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StuController {
    @Autowired
    private StuService stuService;

    //分页查询
    @GetMapping
    public Result page(StuQueryParam stuQueryParam){

        PageResult pageResult = stuService.page(stuQueryParam);
        log.info("-------------"+stuQueryParam.getClazzId());
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        Student student = stuService.getById(id);
        return Result.success(student);
    }
    //添加学员信息
    @PostMapping
    public Result add(@RequestBody Student student){
        stuService.save(student);
        return Result.success();
    }

    //修改学员信息
    @PutMapping
    public Result update(@RequestBody Student student){
        stuService.save(student);
        return Result.success();
    }

    //违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id ,@PathVariable Short score){
        Student student = stuService.getById(id);
//        int i =;
        student.setViolationCount((short) (student.getViolationCount() + 1));
        student.setViolationScore((short)(student.getViolationScore()+(int)(score)));
        log.info("student.getViolationScore():"+student.getViolationScore());
        stuService.save(student);
        return Result.success();
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        stuService.delete(ids);
        return Result.success();
    }


}
